<template>
	<view class="page">
		<image src="/static/accreditBg.png" class="accreditBg" />
		<view class="content">
			<view class="plr50 mt100">
				<view class="shadow border-r1 bf">
					<view class="ptb70 flex_aj">
						<!-- 云禾换电 -->
						<view>
							<image src="/static/mbile-full.png" style="width:258rpx;" mode="widthFix"
								class="border-r1 logo" />
						</view>
						<!-- 太航充电柜 -->
						<!-- <view>
							<image src="/static/logotaihang.png" style="width:258rpx;" mode="widthFix"
								class="border-r1 logo" />
						</view> -->
						<!-- 顺鼎物联 -->
						<!-- <view>
							<image src="/static/logoshunding.png" style="width:258rpx;" mode="widthFix"
								class="border-r1 logo" />
						</view> -->
						<!-- 中电充换 -->
						<!-- <view>
							<image src="/static/logozdch.jpg" style="width:258rpx;" mode="widthFix"
								class="border-r1 logo" />
						</view> -->
					</view>
					<view class="plr24">
						<view>小程序将获得以下授权:</view>
						<view class="c9">获得您的公开信息（昵称、头像等）</view>
					</view>
					<view class="ptb70 flex_a plr60">
						<button class="flex1 reject cf border-r6" @click="reject">拒绝</button>
						<!-- #ifdef MP-WEIXIN -->
						<button open-type="getUserInfo" @getuserinfo="getuserinfo"
							class="flex1 ml40 b-primary lh90 btn_tap cf border-r6 ft30">允许</button>
						<!-- #endif -->
						<!-- #ifdef MP-ALIPAY -->
						<button open-type="getAuthorize" @click="GetAlipayUserInfo" scope="userInfo"
							class="flex1 ml40 b-primary lh90 btn_tap cf border-r6 ft30">允许</button>
						<!-- #endif -->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				openId: ''
			}
		},
		onLoad(e) {
			console.log(e)
			this.openId = e.openId
			uni.setNavigationBarTitle({
				title: this.system == 'wechat' ? '微信授权登录' : '支付宝授权登录'
			})
		},
		onShow() {},
		computed: {
			system() {
				return uni.getSystemInfoSync().app == 'alipay' ? 'alipay' : 'wechat'
			}
		},
		methods: {
			reject() {
				if (getCurrentPages().length <= 2) {
					return uni.reLaunch({
						url: '/pages/user/index'
					})
				}
				uni.navigateBack({
					delta: 2,
					fail: err => {
						console.log('fail')
						uni.reLaunch({
							url: '/pages/user/index'
						})
					}
				})
			},
			async getuserinfo(e) {
				let [b, {
					userInfo
				}] = await uni.getUserInfo()
				//console.log(userInfo);
				this.login({
					nickName: userInfo.nickName,
					avatarUrl: userInfo.avatarUrl,
					openId: this.openId,
					gender: userInfo.gender || 0,
					province: userInfo.province,
					city: userInfo.city
				})
			},
			async GetAlipayUserInfo(e) {
				my.getOpenUserInfo({
					success: res => {
						let userInfo = JSON.parse(res.response).response
						//console.log(userInfo);
						this.login({
							nickName: userInfo.nickName,
							avatarUrl: userInfo.avatar,
							openId: this.openId,
							gender: userInfo.gender == 'm' ? 1 : 2,
							province: userInfo.province,
							city: userInfo.city
						})
					},
					fail: err => {
						console.log(err)
					}
				})
			},
			async login(data) {
				let res = await this.$http({
					url: '/api/user/userRegister',
					data
				})
				console.log(res)
				this.$store.dispatch('handleRegister', res.data)
				uni.navigateBack({
					delta: 1,
					fail: err => {
						uni.reLaunch({
							url: '/pages/user/index/index'
						})
					}
				})
			}
		}
	}
</script>
<style scoped lang="scss">
	.page {
		width: 100vw;
		height: 100vh;

		.accreditBg,
		.content {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}

		.reject {
			background: #ccc;
		}
	}
</style>